﻿using RimWorld;
using Verse;
using Verse.AI;

namespace Miao.Stand
{
    /// <summary>
    /// 替身环绕主人
    /// </summary>
    public class JobGiver_StandSurroundMaster : JobGiver_Wander
    {
        public JobGiver_StandSurroundMaster()
        {
            ticksBetweenWandersRange = new IntRange(50, 200);

        }

        protected override Job TryGiveJob(Pawn pawn)
        {
            StandPawn standPawn = (pawn as StandPawn);
            wanderRadius = standPawn.WanderRanage;
            
            //Messages.Message("重新执行游荡", MessageTypeDefOf.NeutralEvent);
            if (pawn.Position.InHorDistOf(GetWanderRoot(pawn), wanderRadius))
            {
                pawn.mindState.nextMoveOrderIsWait = !pawn.mindState.nextMoveOrderIsWait;
                if (pawn.mindState.nextMoveOrderIsWait)
                {
                    Job wait = JobMaker.MakeJob(StandJobDefOf.StandWait);
                    
                    wait.expiryInterval = ticksBetweenWandersRange.RandomInRange;
                    return wait;
                }
                else
                {
                    IntVec3 wanderDest = GetExactWanderDest(pawn);
                    Job movejob = JobMaker.MakeJob(JobDefOf.Goto, wanderDest);
                    return movejob;
                }
            }
            else
            {
                //var follow = JobMaker.MakeJob(JobDefOf.FollowClose, pawn);//这一套使用起来有点问题
                //follow.followRadius = wanderRadius;
                //return follow;
                IntVec3 wanderDest = GetExactWanderDest(pawn);
                return JobMaker.MakeJob(JobDefOf.Goto, wanderDest);
                
            }


            //bool flag = pawn.CurJob != null && pawn.CurJob.def == JobDefOf.GotoWander;//如果工作不等于空并且工作等于去游荡
            //bool nextMoveOrderIsWait = pawn.mindState.nextMoveOrderIsWait;
            //if (!flag)//这应该是锁，用于轮换等待和移动
            //{
            //	pawn.mindState.nextMoveOrderIsWait = !pawn.mindState.nextMoveOrderIsWait;//下一步是等待
            //}
            //if (nextMoveOrderIsWait && !flag)//这段可能是移一定距离呆在原地楞一段距离
            //{
            //	Messages.Message("待机执行", MessageTypeDefOf.NeutralEvent);
            //	Job job = JobMaker.MakeJob(JobDefOf.Wait_Wander);
            //	job.expiryInterval = ticksBetweenWandersRange.RandomInRange;//漫游有效期
            //	return job;//有效期超过返回等待命令
            //}
            //IntVec3 exactWanderDest = GetExactWanderDest(pawn);//精确漫游目的地，在目的地旁添加漫游点确定
            //if (!exactWanderDest.IsValid)
            //{
            //	pawn.mindState.nextMoveOrderIsWait = false;
            //	return null;
            //}
            //LocomotionUrgency value = locomotionUrgency;//标准移动模式
            //if (locomotionUrgencyOutsideRadius.HasValue && !pawn.Position.InHorDistOf(GetWanderRoot(pawn), wanderRadius))//如果超出半径
            //{
            //	value = locomotionUrgencyOutsideRadius.Value;//超出的半径移动模式 
            //}
            //Messages.Message("移动执行", MessageTypeDefOf.NeutralEvent);
            //Job job2 = JobMaker.MakeJob(JobDefOf.GotoWander, exactWanderDest);
            //job2.locomotionUrgency = value;
            //job2.expiryInterval = expiryInterval;
            //job2.checkOverrideOnExpire = true;
            //return job2;
        }


        protected override IntVec3 GetWanderRoot(Pawn pawn)
        {
            return (pawn as StandPawn).owner.Position;
        }
    }
}
